package jeons.buildqna.action;

import jeons.admin.buildproduct.dto.BuildQnADTO;
import jeons.buildqna.biz.BuildQnaBiz;
import jeons.common.util.PageDTO;
import jeons.common.util.PagingHelper;

import org.springbyexample.web.servlet.view.tiles2.TilesUrlBasedViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/buildqna")
public class BuildQnaAction {
	@Autowired TilesUrlBasedViewResolver tilesUrlBasedViewResolver;
	@Autowired BuildQnaBiz buildQnaBiz;
	
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public String index(Model model,PageDTO pageDTO,@RequestParam("bdno")int bdno,@ModelAttribute("buildQnADTO")BuildQnADTO buildQnADTO){
		tilesUrlBasedViewResolver.setTilesDefinitionName("tmpl");
		System.out.println("인덱스");
		buildQnADTO.setBdno(bdno);
		int count = buildQnaBiz.getCount(buildQnADTO);
		model.addAttribute("count", count);
		model.addAttribute("qnadata", buildQnaBiz.getList(buildQnADTO));
		System.out.println("aaaaaaaaaaaaa");
		model.addAttribute("page",PagingHelper.instance.autoPaging(count, pageDTO.getPlRowRange(), pageDTO.getPlPageRange(), buildQnADTO.getPage()));
		return "buildqna/index";
	}
	
	
	@RequestMapping(value="/insert", method=RequestMethod.GET)
		public String insert(Model model,@RequestParam("bdtype")String bdtype,@RequestParam("bdno")int bdno,@RequestParam(value="qnano", required=false,  defaultValue="0")int qnano){
			tilesUrlBasedViewResolver.setTilesDefinitionName("tmpl");
		if (qnano == 0) {
			model.addAttribute("qna", new BuildQnADTO());
			model.addAttribute("bdtype", bdtype);
			model.addAttribute("bdno", bdno);
		} else {
			model.addAttribute("qna", buildQnaBiz.getData(qnano));
		}
			return "buildqna/insert";
		}
	
	@RequestMapping(value="/insert", method=RequestMethod.POST)
	public void insertData(@ModelAttribute("qna")BuildQnADTO buildQnADTO){
		if (buildQnADTO.getQnano() == null) {
			buildQnaBiz.insertData(buildQnADTO);
		} else {
			buildQnaBiz.updateData(buildQnADTO);	
		}
	}
	
	
	@RequestMapping(value="/checkpasswd", method=RequestMethod.GET)
	public String checkPassword(Model model,@RequestParam("qnano")int qnano,@RequestParam("board_type")String board_type){
		tilesUrlBasedViewResolver.setTilesDefinitionName("tmpl");
		if (board_type.equals("modify")) {
			model.addAttribute("board_type", board_type);
		}
		model.addAttribute("board_type", board_type);
		model.addAttribute("data", buildQnaBiz.getData(qnano));
		return "buildqna/checkpasswd";
	}
	
/*	@RequestMapping(value="/checkpasswd", method=RequestMethod.GET)
	public String checkPassword(Model model,@RequestParam("qnano")int qnano,@RequestParam("board_type")String modify){
		tilesUrlBasedViewResolver.setTilesDefinitionName("tmpl");
		model.addAttribute("modify", modify)
		model.addAttribute("data", buildQnaBiz.getData(qnano));
		return "buildqna/checkpasswd";
	}
	*/
	@RequestMapping(value="/view", method=RequestMethod.GET)
	public String view(Model model,@RequestParam("qnano")int qnano){
		model.addAttribute("data", buildQnaBiz.getData(qnano));
		model.addAttribute("reply", buildQnaBiz.getReply(qnano));
		return "buildqna/view";
	}
	
	
	@RequestMapping(value="/test", method=RequestMethod.GET)
	public String test(){
		
		return "buildqna/test";
	}
	
	@RequestMapping(value="/deletedata", method=RequestMethod.GET)
	public @ResponseBody String deleteData(@RequestParam("qnano")int qnano){
		System.out.println("삭제를 탔습니다.");
		String check = "true";
		try {
			System.out.println("삭제 아래");
			buildQnaBiz.deleteReplyData(qnano);
			buildQnaBiz.deleteData(qnano);
		} catch (Exception e) {
			// TODO: handle exception
			check = "faluse";
		}
		return check;
	}
}
